package com.dd.cloud.user.controller;

import com.dd.cloud.common.exception.ZekeOpenApiException;
import com.dd.cloud.user.DTO.CallbackPresStatusReq;
import com.dd.cloud.user.DTO.CommonParamReq;
import com.dd.cloud.user.DTO.common.*;
import com.dd.cloud.user.DTO.kz.KzSyncPharmacyDataDTO;
import com.dd.cloud.user.DTO.kz.KzSyncPharmacyDrugDTO;
import com.dd.cloud.user.DTO.kz.KzSyncPharmacyDrugStockDTO;
import com.dd.cloud.user.service.open.IOpenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author Lx
 * @version 1.0
 * @date 2023/8/16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/open/api")
public class OpenApiController {
    @Autowired
    private IOpenApiService openApiService;

    /**
     * 获取授权token
     *
     * @param dto
     * @return
     */
    @PostMapping("/get/access/token")
    public CommonResDTO<ResultAccessTokenDTO> getAccessToken(@Valid @RequestBody GetAccessTokenDTO dto) {
        log.info("dto:{}", dto);
        return openApiService.getAccessToken(dto);
    }

    /**
     * 验证加密数据(测试使用)
     *
     * @param dto
     * @return
     */
    @PostMapping("/verify/encryption/data")
    public CommonResDTO<String> verifyEncryptionData(@Valid @RequestBody VerifyEncryptionDataDTO dto) {
        log.info("dto:{}", dto);
        return openApiService.verifyEncryptionData(dto);
    }

    /**
     * 生成加密数据(测试使用)
     *
     * @param dto
     * @return
     */
    @PostMapping("/generate/encryption/data")
    public CommonResDTO<String> generateEncryptionData(@Valid @RequestBody GenerateEncryptionDataDTO dto) {
        log.info("dto:{}", dto);
        return openApiService.generateEncryptionData(dto);
    }

    /**
     * 上传同步药房门店数据
     *
     * @param dto
     * @return
     */
    @PostMapping("/sync/pharmacy/data")
    public CommonResDTO<String> kzSyncPharmacyData(HttpServletRequest request, @Valid @RequestBody KzSyncPharmacyDataDTO dto) {
        log.info("dto:{}", dto);
        return openApiService.kzSyncPharmacyData(request, dto);
    }

    /**
     * 上传同步药房门店药材数据
     *
     * @param dto
     * @return
     */
    @PostMapping("/sync/pharmacy/drug/data")
    public CommonResDTO<String> kzSyncPharmacyDrug(HttpServletRequest request, @Valid @RequestBody KzSyncPharmacyDrugDTO dto) {
        return new CommonResDTO<>();
    }

    /**
     * 上传同步药房门店药材库存数据
     *
     * @param dto
     * @return
     */
    @PostMapping("/sync/pharmacy/drug/stock/data")
    public CommonResDTO<String> kzSyncPharmacyDrugStock(HttpServletRequest request, @Valid @RequestBody KzSyncPharmacyDrugStockDTO dto) {
        return new CommonResDTO<>();
    }

    /**
     * 通用状态回调数据
     *
     * @param request
     * @return
     */
    @GetMapping("/status/report/data")
    public CommonResDTO<String> CommonStatusReportReq(HttpServletRequest request) {
        CallbackPresStatusReq req = new CallbackPresStatusReq("common", request);
        return new CommonResDTO<>();
    }

    /**
     * 通用物流回调数据
     *
     * @param request
     * @return
     */
    @GetMapping("/express/report/data")
    public CommonResDTO<String> CommonExpressReportReq(HttpServletRequest request) {
        CommonParamReq req = new CommonParamReq("common", request);
        return new CommonResDTO<>();
    }

    /**
     * 处理对内接口系统业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ZekeOpenApiException.class)
    public CommonResDTO<Object> handleZekeOpenApiException(ZekeOpenApiException e) {
        CommonResDTO<Object> res = new CommonResDTO<>();
        res.setCode(e.getCode());
        res.setCodeMsg(e.getCodeMsg());
        if (e.getData() != null) {
            res.setData(e.getData());
        } else {
            res.setData("fail");
        }
        return res;
    }
}
